﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {
    import flash.events.*;
    import Engine.Interfaces.*;
    import Engine.Init.*;
    import flash.utils.*;
    import __AS3__.vec.*;

    public class PopupQueueManager {

        private static var m_instance:PopupQueueManager = null;
        private static var m_canInstantiate:Boolean = false;

        private var m_popupQueue:Vector.<IDialog>;
        private var m_popupStack:Vector.<IDialog>;

        public function PopupQueueManager(){
            this.m_popupQueue = new Vector.<IDialog>();
            this.m_popupStack = new Vector.<IDialog>();
            super();
            if (!m_canInstantiate){
                throw (new Error("Cannot call PopupQueueManager constructor directly - use getInstance() instead."));
            };
        }
        public static function getInstance():PopupQueueManager{
            if (null == m_instance){
                m_canInstantiate = true;
                m_instance = new (PopupQueueManager)();
                m_canInstantiate = false;
            };
            return (m_instance);
        }

        public function showDialog(_arg1:IDialog, _arg2:Boolean=true):void{
            var _local4:IDialog;
            var _local5:IDialog;
            var _local3:Boolean;
            if (_arg1.unique == true){
                for each (_local4 in this.m_popupStack) {
                    if (_arg1.dialogName == _local4.dialogName){
                        _local3 = false;
                        break;
                    };
                };
                for each (_local5 in this.m_popupQueue) {
                    if (_arg1.dialogName == _local5.dialogName){
                        _local3 = false;
                        break;
                    };
                };
            };
            if (_local3){
                if (_arg2){
                    this.m_popupQueue.push(_arg1);
                    this.pumpPopupQueue();
                } else {
                    if (FreezeManager.getInstance().isAnythingFrozen() == false){
                        this.displayImmediately(_arg1);
                    } else {
                        this.m_popupQueue.unshift(_arg1);
                    };
                };
            };
        }
        public function closeShownDialogs():void{
            var _local1:IDialog;
            for each (_local1 in this.m_popupStack) {
                _local1.close();
            };
            this.m_popupStack = new Vector.<IDialog>();
        }
        private function displayImmediately(_arg1:IDialog):void{
            var _local2:IDialog;
            if (this.m_popupStack.length > 0){
                _local2 = this.m_popupStack[(this.m_popupStack.length - 1)];
                _local2.disable();
            };
            GlobalEngine.stage.addChild(_arg1.getDisplayObject());
            _arg1.getDisplayObject().addEventListener(Event.CLOSE, this.onPopupClosed, false, 0, true);
            this.m_popupStack.push(_arg1);
            _arg1.show();
            FreezeManager.getInstance().freezeRender();
        }
        private function onPopupClosed(_arg1:Event):void{
            var _local2:IDialog;
            _arg1.currentTarget.removeEventListener(Event.CLOSE, this.onPopupClosed);
            this.m_popupStack.pop();
            if (this.m_popupStack.length > 0){
                _local2 = this.m_popupStack[(this.m_popupStack.length - 1)];
                _local2.enable();
            } else {
                this.pumpPopupQueue();
            };
            FreezeManager.getInstance().thaw(true);
        }
        public function pumpPopupQueue():void{
            var _local1:IDialog;
            if ((((((this.m_popupQueue.length > 0)) && ((this.m_popupStack.length == 0)))) && (InitializationManager.getInstance().haveAllCompleted()))){
                _local1 = this.m_popupQueue.shift();
                this.displayImmediately(_local1);
            };
        }
        public function hasActiveDialog():Boolean{
            return ((this.m_popupStack.length > 0));
        }
        public function getActiveDialog():IDialog{
            var _local1:IDialog;
            if (this.hasActiveDialog()){
                _local1 = this.m_popupStack[(this.m_popupStack.length - 1)];
            };
            return (_local1);
        }
        public function getActiveDialogName():String{
            var _local1 = "";
            var _local2:IDialog = this.getActiveDialog();
            if (_local2 != null){
                _local1 = getQualifiedClassName(_local2);
                if (_local1.length >= 39){
                    _local1 = _local1.substr((_local1.lastIndexOf(".") + 1));
                    if (_local1.length >= 39){
                        _local1 = _local1.substr((_local1.indexOf("::") + 2));
                    };
                };
                _local1 = ("popup:" + _local1);
            } else {
                _local1 = "unkownUI";
            };
            return (_local1);
        }

    }
}//package Engine.Managers 
